<?php

class PagesController extends Osml_Controller_Action
{

    public function voirAction(){
        $request = $this->getRequest();
        $id = $request->getParam("id", 0);
        $page = new Back_Model_Pages($id);
        
        $matches = array();
        $contenu = $page->getContenu();
        $accents = "àáâãäåçèéêëìíîïðòóôõöùúûüýÿ";
        $accentsHtml = htmlentities($accents);
        preg_match_all("/!## AFFICHER_EQUIPE\(##([\w\s\(\)àáâãäåçèéêëìíîïðòóôõöùúûüýÿ$accentsHtml]*)##\) ##\!/", $contenu, $matches);
        
//         Zend_Debug::dump($matches);
        
        /*
         * Matches contient ce que le patterne à capturé en 0 et en 1 le motif demandé soit le nom de l'équipe
         * 
         */
        $indice = 0;
        foreach( $matches[1] as $nomEquipe ) {
            $equipeId = Back_Model_Equipes::recuperationIdParValeur(html_entity_decode($nomEquipe,ENT_COMPAT,"UTF-8"));
            if (empty($equipeId)) {
                $remplacement[$indice] = sprintf(__("Equipe non trouvée '%s'"), $nomEquipe);
                continue;
            }
            $equipe = new Back_Model_Equipes($equipeId);
            $this->view->model = $equipe;
            
            $html = $this->view->render("pages/templates/afficher-equipe.phtml");
            $remplacement[$indice] = $html;
            $indice++; 
        }
        
        $indice =0;
        foreach($matches[0] as $chaine) {
            $chaine = preg_replace("/!/", "\\!", $chaine);
            $chaine = preg_replace("/\(/", "\\(", $chaine);
            $chaine = preg_replace("/\)/", "\\)", $chaine);
            $contenu = preg_replace("/$chaine/", $remplacement[$indice], $contenu);
            $indice++;
        }
        
        $page->setContenu($contenu);
        
        $this->view->model = $page;
        $this->view->titrePage = $page->getTitre();
    }
    
}

